package com.mvc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer.PageSize;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * 
 * @author snowpbx
 * @version V1.0 Create Time: 2012-10-14
 */

public class PdfUtils {
	protected static final String	TMP_DIR	= System.getProperty("java.io.tmpdir");

	private static final Logger	  LOGGER	= Logger.getLogger(PdfUtils.class);

	public static void main(String[] args) throws Exception {
		StringBuilder html = new StringBuilder();
		html.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
		html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("<head>");
		html.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">");
		html.append("<style type=\"text/css\">body {font-family:SimSun;}</style>").append("</head>").append("<body>");
		html.append("<div style=\"color:blue;font-family:'长城中楷体繁';\">東莞市優潤紙品有限公司！1234567890abcdefghi</div>");
		html.append("<div style=\"color:blue;font-family:DFPBiaoKaiShu-B5;\">東莞市優潤紙品有限公司！1234567890abcdefghi</div>");
		html.append("<div style=\"color:blue;font-family:KaiTi_GB2312;\">東莞市優潤紙品有限公司！1234567890abcdefghi</div>");
		html.append("</body></html>");
		System.out
		        .println("SS: "
		                + htmlToPdf(
		                        html.toString(),
		                        null,
		                        "Street, Dalingshan Town, Dongguan City,Guangdong.China.<br/>Website:WWW.xinjieyuan.com.hk Tel:852-6646-2916 Mail:Martin.lee@xinjieyuan.com.hk"));
	}

	private static CYaHPConverter	converter	= new CYaHPConverter();
	public static final PageSize	MY_A4P	  = new PageSize(20.8d, 29.6d, 1d, 1d, 1.5d, 1d);

	public static File htmlToPdf(String content, String header, String footer) throws Exception {
		File file = new File(TMP_DIR + File.separator + System.currentTimeMillis() + ".pdf");
		OutputStream out = new FileOutputStream(file);
		Map properties = new HashMap();
		properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
		properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "d:/fonts");
		List headerFooters = new ArrayList();
		if (StringUtils.isNotBlank(header)) {
			headerFooters.add(new IHtmlToPdfTransformer.CHeaderFooter(header, IHtmlToPdfTransformer.CHeaderFooter.HEADER));
		}
		if (StringUtils.isNotBlank(footer)) {
			headerFooters.add(new IHtmlToPdfTransformer.CHeaderFooter(footer, IHtmlToPdfTransformer.CHeaderFooter.FOOTER));
		}
		converter.convertToPdf(content, IHtmlToPdfTransformer.A4P, headerFooters, null, out, properties);
		LOGGER.debug("Convert finished:" + file.getAbsolutePath());
		out.flush();
		out.close();
		return file;
	}
	// public static File htmlToPdf0(String body) throws Exception {
	// StringBuilder html = new StringBuilder();
	// html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	// html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("<head>")
	// .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></meta><body>");
	// html.append(body).append("</body></html>");
	// File file = new File(TMP_DIR + File.separator +
	// System.currentTimeMillis() + ".pdf");
	// OutputStream os = new FileOutputStream(file);
	// ITextRenderer render = new ITextRenderer();
	// ITextFontResolver font = render.getFontResolver();
	// try {
	// font.addFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	// //
	// font.addFont(Html2Pdfs.class.getResource("SIMSUN.TTC").toString().substring(6),
	// // BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
	// } catch (com.itextpdf.text.DocumentException e) {
	// e.printStackTrace();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// render.setDocumentFromString(html.toString());
	// render.layout();
	// render.createPDF(os);
	// os.close();
	// return file;
	// }

	// public static File htmlToPdf1(String body) throws Exception {
	// StringBuffer html = new StringBuffer();
	// html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	// html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("<head>")
	// .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><body>");
	// html.append(body).append("</body></html>");
	// ITextRenderer renderer = new ITextRenderer();
	// renderer.setDocumentFromString(html.toString());
	// renderer.layout();
	// File file = new File(TMP_DIR + File.separator +
	// System.currentTimeMillis() + ".pdf");
	// OutputStream os = new FileOutputStream(file);
	// renderer.createPDF(os);
	// // Finishing up
	// renderer.finishPDF();
	// os.flush();
	// os.close();
	// return file;
	// }

}
